package com.netdisk.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.netdisk.entity.Favorite;
import com.netdisk.entity.File;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 收藏Mapper接口
 */
@Mapper
public interface FavoriteMapper extends BaseMapper<Favorite> {

    /**
     * 直接SQL方式查询用户收藏的文件
     */
    @Select("SELECT f.* FROM files f JOIN favorites fav ON f.id = fav.file_id " +
            "WHERE fav.user_id = #{userId} AND f.deleted_at IS NULL " +
            "ORDER BY fav.created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<File> selectFavoriteFilesDirectSQL(@Param("userId") Integer userId, @Param("limit") Integer limit, @Param("offset") Integer offset);
    
    /**
     * 带搜索功能的直接SQL查询用户收藏的文件
     */
    @Select("SELECT f.* FROM files f JOIN favorites fav ON f.id = fav.file_id " +
            "WHERE fav.user_id = #{userId} AND f.deleted_at IS NULL " +
            "AND f.name LIKE CONCAT('%', #{query}, '%') " +
            "ORDER BY fav.created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<File> selectFavoriteFilesWithQueryDirectSQL(@Param("userId") Integer userId, @Param("limit") Integer limit, @Param("offset") Integer offset, @Param("query") String query);
    
    /**
     * 计数用户收藏文件总数
     */
    @Select("SELECT COUNT(*) FROM files f JOIN favorites fav ON f.id = fav.file_id " +
            "WHERE fav.user_id = #{userId} AND f.deleted_at IS NULL")
    int countFavoriteFiles(@Param("userId") Integer userId);
    
    /**
     * 带搜索功能的计数用户收藏文件总数
     */
    @Select("SELECT COUNT(*) FROM files f JOIN favorites fav ON f.id = fav.file_id " +
            "WHERE fav.user_id = #{userId} AND f.deleted_at IS NULL " +
            "AND f.name LIKE CONCAT('%', #{query}, '%')")
    int countFavoriteFilesWithQuery(@Param("userId") Integer userId, @Param("query") String query);
}